package cz.muni.fi.pa165.entity;

import cz.muni.fi.pa165.dto.PackageDTO;
import java.io.Serializable;
import java.math.BigDecimal;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

/**
 * This class represents a package to be delivered
 * 
 * @author Ondrej Cernay
 */
@Entity
public class Package implements Serializable {
    private static final long serialVersionUID = 1L;
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    
    /**
     * Package width in centimeters
     */
    private int width;
    
    /**
     * Package height in centimeters
     */
    private int height;

    /**
     * Package weight in grams
     */
    private int weight;
    
    /**
     * Value of the package
     */
    private BigDecimal price;

    /**
     * 
     * @return Value of the package
     */
    public BigDecimal getPrice() {
        return price;
    }

    /**
     * Sets the value of the package
     * @param price Package value
     * @throws IllegalArgumentException if price is a negative number
     */
    public void setPrice(BigDecimal price) {
        if (price.compareTo(BigDecimal.ZERO) < 0) {
            throw new IllegalArgumentException("Price must be greater or equal to zero");
        }
        this.price = price;
    }
    
    /**
     * @return Width of the package in cm
     */
    public int getWidth() {
        return width;
    }

    /**
     * Sets the width of the package
     * @param width Package width in cm
     * @throws IllegalArgumentException if width is equal to zero or lower
     */
    public void setWidth(int width) {
        if (width <= 0) {
            throw new IllegalArgumentException("Width must be greater than to zero");
        }
        this.width = width;
    }

    /**
     * @return Package height in cm
     */
    public int getHeight() {
        return height;
    }

    /**
     * Sets package height
     * @param height Package height in cm
     * @throws IllegalArgumentException if height is equal to zero or lower
     */
    public void setHeight(int height) {
        if (height <= 0) {
            throw new IllegalArgumentException("Height must be greater than zero");
        }
        this.height = height;
    }

    /**
     * @return Package weight in grams
     */
    public int getWeight() {
        return weight;
    }

    /**
     * Sets package weight
     * @param weight Package weight in grams
     * @throws IllegalArgumentException if weight is equal to zero or lower
     */
    public void setWeight(int weight) {
        if (weight <= 0) {
            throw new IllegalArgumentException("Weight must be greater than zero");
        }
        this.weight = weight;
    }
    
    /**
     * @return Id of the package
     */
    public Long getId() {
        return id;
    }

    /**
     * Sets unique id of the package
     * @param id Package id
     * @throws IllegalArgumentException if id was set before method called
     */
    public void setId(Long id) {
        if (this.id != null) {
            throw new IllegalArgumentException("id was already set.");
        }
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Package other = (Package) obj;
        if (this.id == null || !this.id.equals(other.id)) {
            return false;
        }
        return true;
    }  

    @Override
    public String toString() {
        return "cz.muni.fi.pa165.Package[ id=" + id + ", width=" + width + "cm, height=" + height + "cm, weight=" + weight + "g, price="+price+" ]";
    }
    
}
